package com.lyw.controller;

import com.lyw.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

@Controller
public class IndexController {

  @GetMapping({"/", "/login"})
  public String login() {
    return "login";
  }

  @PostMapping("/login")
  public String main(User user, HttpSession session, Model model) {
    //HttpSession  session = request.getSession();
    if (!StringUtils.isEmpty(user.getUsername()) && "123456".equals(user.getPasswd()))
    {
      session.setAttribute("user", user);
      //防止表单重复提交
      return "redirect:/main.html";
    }

    model.addAttribute("meg", "用户名账户或密码错误，请重新登录！");
    //request.setAttribute("meg", "用户名账户或密码错误，请重新登录！");
    return "/login";
  }

  @GetMapping("/main.html")
  public String index() {
    return "main";
  }

}
